<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="10"
          ><div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="pages.query"
              class="input-with-select"
              @input="search"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getgoods"
              ></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="14"
          ><div class="grid-content bg-purple-light">
            <el-button type="primary" @click="jumpaddshop">添加商品</el-button>
          </div></el-col
        >
      </el-row>
      <el-table :data="shoplist" style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="120">
        </el-table-column>

        <el-table-column prop="goods_price" label="商品价格（元）">
        </el-table-column>

        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>

        <el-table-column prop="goods_number" label="商品数量">
        </el-table-column>

        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | timeC }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              class="el-icon-edit"
              @click="edit(scope.row)"
              type="primary"
              size="small"
            ></el-button>
            <el-button
              type="danger"
              size="small"
              class="el-icon-delete"
              @click="del(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pages.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pages.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total * 1"
      >
      </el-pagination>
    </el-card>
    <!-- 编辑 -->
    <el-dialog title="编辑商品" :visible.sync="dialogVisible" width="50%">
      <!-- <span>这是一段信息</span> -->
      <el-form ref="form" :model="editform" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="editform.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="editform.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input v-model="editform.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="editform.goods_number"></el-input>
        </el-form-item>
        <el-form-item>
          <!-- @click="onSubmit" -->
          <el-button type="primary" @click="editok">立即修改</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import _ from 'lodash'
import { GetgoodsApi, editshopApi, delshopApi } from '@/http/api'
export default {
  name: 'goodsA',
  data() {
    return {
      total: '',
      shoplist: [],
      pages: {
        query: '',
        pagenum: 1,
        pagesize: 20
      },
      dialogVisible: false,
      form: {},
      editform: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: ''
      }
    }
  },
  created() {
    this.getgoods()
  },
  methods: {
    // 搜索
    search: _.debounce(function () {
      this.getgoods()
    }, 300),
    // 删除
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log(row.goods_id)
          const res = await delshopApi(row.goods_id)
          console.log(res)
          this.getgoods()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑
    edit(row) {
      console.log(row)
      this.dialogVisible = true
      this.editform.goods_name = row.goods_name
      this.editform.goods_weight = row.goods_weight
      this.editform.goods_price = row.goods_price
      this.editform.goods_number = row.goods_number
      this.editform.id = row.goods_id
    },
    async editok() {
      this.dialogVisible = false
      const res = await editshopApi(this.editform)
      console.log(res)
    },
    // 渲染
    async getgoods() {
      const res = await GetgoodsApi(this.pages)
      const { total, goods } = res
      this.total = total
      this.shoplist = goods
      // goods.forEach((item) => {
      //   this.shoplist.add_time = new Date(item.add_time)
      // })
      console.log(res)
    },
    // 跳转
    jumpaddshop() {
      this.$router.push('/addshop')
    },
    // 分页
    handleCurrentChange(val) {
      this.pages.pagenum = val
      this.getgoods()
    },
    handleSizeChange(val) {
      this.pages.pagesize = val
      this.getgoods()
    }
  }
}
</script>
